package dao;

import common.spring.jdbc.MySQLHelper;
import common.utils.MyMapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import pojo.PayInfo;
import pojo.form.PayInfoAddForm;
import pojo.form.PayInfoQueryForm;
import utils.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by asus on 2018/10/31.
 */
@Repository
public class PayInfoDao {
    @Autowired
    MySQLHelper mySQLHelper;

    public int addPayInfo(PayInfoAddForm addForm) {
        return mySQLHelper.simpleInsert("pay_info", MyMapUtils.beanToMap(addForm, false));
    }

    public PayInfo getPayInfo(String orderCode) {
        return mySQLHelper.queryForBean("select * from pay_info where orderCode=? ", PayInfo.class, new Object[]{orderCode});
    }

    public List<PayInfo> getPayInfo(PayInfoQueryForm queryForm) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("select * from pay_info where 1=1 ");
        List list = new ArrayList();
        if (StringUtils.isNotBlank(queryForm.getClientNotifyUrl())) {
            stringBuilder.append(" and clientNotifyUrl = ? ");
            list.add(queryForm.getClientNotifyUrl());
        }
        if (StringUtils.isNotBlank(queryForm.getOrderCode())) {
            stringBuilder.append(" and orderCode= ? ");
            list.add(queryForm.getOrderCode());
        }
        if (StringUtils.isNotBlank(queryForm.getPayPlatform())) {
            stringBuilder.append(" and payPlatform= ? ");
            list.add(queryForm.getPayPlatform());
        }
        if (org.apache.commons.lang.StringUtils.isNotBlank(queryForm.getStartTime())) {
            queryForm.setStartTime(queryForm.getStartTime().substring(0, 10) + " 00:00:00");
            stringBuilder.append(" and createTime >= ? ");
            list.add(queryForm.getStartTime());
        }
        if (org.apache.commons.lang.StringUtils.isNotBlank(queryForm.getEndTime())) {
            queryForm.setEndTime(queryForm.getEndTime().substring(0, 10) + " 23:59:59");
            stringBuilder.append(" and createTime <= ? ");
            list.add(queryForm.getEndTime());
        }
        if (queryForm.getIsPay() > 0) {
            stringBuilder.append(" and isPay= ? ");
            list.add(queryForm.getIsPay());
        }
        return mySQLHelper.queryForListBean(stringBuilder.toString(), PayInfo.class, list.toArray());
    }

    public int updatePayInfo(String orderCode, String transactionId,String orderDes,int totalFee) {
        return mySQLHelper.update("update pay_info set payTime=?,isPay=1,totalFee=?,transactionId=?,orderDes=? where orderCode =? ", new Object[]{new Date(),totalFee, transactionId,orderDes, orderCode});
    }

    public int updatePrepay_id(String prepay_id,String orderCode){
        return mySQLHelper.update("update pay_info set prepay_id=? where orderCode=? ",new Object[]{prepay_id,orderCode});
    }

    public int refund(String orderCode,Double amount,String refundCode){
        return mySQLHelper.update("update pay_info set isPay=3,refundFee=?,refundTime=?,refundCode=? where orderCode =?",new Object[]{amount,new Date(),refundCode,orderCode});
    }
}
